package wiki.liven.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import wiki.liven.pojo.bean.ClickData;
import wiki.liven.pojo.bean.VistersData;
import wiki.liven.mapper.ClickDataMapper;
import wiki.liven.mapper.VistersDataMapper;
import wiki.liven.service.IClickDataService;
import wiki.liven.utils.TimeUtils;

import java.text.ParseException;
import java.util.*;

/**
 * Created by qiangjun on 2017/3/2.
 * 提供点播相关服务
 */
@Service
@CacheConfig(cacheNames = "clickData")//在内存中开辟了一块名称为clickData的区域
public class ClickDataServiceImpl extends ServiceImpl<ClickDataMapper,ClickData> implements IClickDataService {

    @Autowired
    private ClickDataMapper mapper;
    @Autowired
    private VistersDataMapper vistersDataMapper;


    public static final Integer ACTIVITY_USER_STANDER = 20;
    public static final Integer HOT_USER_STANDER = 10;



    /**
     * 获得在时间区域中的所有用户信息
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    @Cacheable
    public List<String> getUsers(String startTime,String endTime){
        return mapper.getMacsDistinct(startTime,endTime);
    }

    /**
     * 计算任意用户在时间区间内的点播时长
     * 1.获取用户在时间区间内的点播记录
     * 2.获取用户在时间区间内的浏览记录
     * 3.做差值：每个点播记录的开始时间-该用户的最近3小时之内的的最近的浏览时间
     * 4.差值即为该用户在该段时间内的点播时长
     * 5.因为，点播记录中并没有记录到退出播放的时间，所以存在一定的偏差。
     * @param mac 必须有值
     * @param startTime 必须有值
     * @param endTime 同上
     * @return 点播时长（单位/分）
     */
    @Override
    @Cacheable
    public Long getClickTime(String mac,String startTime,String endTime) throws ParseException {
        Long countsClickTime = 0L;
        List<ClickData> clickDatas = mapper.selectList(
                new EntityWrapper<ClickData>()
                        .eq("mac",mac).between("timePoint",startTime,endTime).orderBy("timePoint",true)
        );
        if (clickDatas == null)
            return 0L;
        for (ClickData data:clickDatas){
            Long clickTime;
            String startTimeClickData = TimeUtils.formatDate(data.getTimePoint());
            List<VistersData> vistersDatas = vistersDataMapper.selectList(
                    new EntityWrapper<VistersData>()
                            .eq("macOfUser",mac).between("vistTime",startTimeClickData, TimeUtils.addHour(startTimeClickData,3))
                            .orderBy("vistTime",true)
            );
            if (vistersDatas!=null&&vistersDatas.size()>0){
                Optional<VistersData> fristVistersData = vistersDatas.stream().findFirst();//java8 新特性stream对象
                clickTime = (data.getTimePoint().getTime()-fristVistersData.get().getVistTime().getTime())*(1000*60);
                countsClickTime+=clickTime;
            }
        }
        return countsClickTime;
    }


    /**
     * 筛选活跃用户：
     * 1.月度热心用户：每月点播次数大于10次并小于20次
     * 2.月度活跃用户：每月点播次数大于20次
     * @param users 初始用户集合
     * @return "activityUsers" 活跃用户集合
     */
    @Override
    @Cacheable
    public List<String> selectActivityUser(List<String> users,String startTime,String endTime){
        List<String> activityUsers = new ArrayList<>();
        for (String user : users){
            Integer clickDataCount = getUserClickDataCount(user,startTime,endTime);
            System.out.println("用户名称:"+user+"---"+"用户点击数量:"+clickDataCount);
            if (clickDataCount>ACTIVITY_USER_STANDER){
                activityUsers.add(user);
            }
        }
        return activityUsers;
    }

    /**
     * 筛选热心用户：
     * 1.月度热心用户：每月点播次数大于10次并小于20次
     * 2.月度活跃用户：每月点播次数大于20次
     * @param users 初始用户集合
     * @return "hotUsers" 热心用户集合
     */
    @Override
    @Cacheable
    public List<String> selectHotUser(List<String> users,String startTime,String endTime){
        List<String> hotUsers = new ArrayList<>();
        for (String user : users){
            Integer clickDataCount = getUserClickDataCount(user,startTime,endTime);
            System.out.println("用户名称:"+user+"---"+"用户点击数量:"+clickDataCount);
            if (clickDataCount>HOT_USER_STANDER&&clickDataCount<ACTIVITY_USER_STANDER){
                hotUsers.add(user);
            }
        }
        return hotUsers;
    }



    /**
     * 获取用户的点播次数
     * @param mac
     * @param starTime
     * @param endTime
     * @return
     */
    public Integer getUserClickDataCount(String mac,String starTime,String endTime){
        Integer clickDataCount;
        List<ClickData> clickDatas = mapper.selectList(
                new EntityWrapper<ClickData>()
                .eq("mac",mac)
                .between("timePoint",starTime,endTime)
        );
        clickDataCount = clickDatas.size();
        return clickDataCount;

    }


}
